package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.AccountPageForm;
import com.woniuxy.controller.form.ChaneAccountPasswordForm;
import com.woniuxy.controller.form.RegistAccountForm;
import com.woniuxy.controller.info.Result;
import com.woniuxy.controller.info.SimpleAccountnfo;
import com.woniuxy.model.Account;
import com.woniuxy.dao.AccountDao;
import com.woniuxy.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.util.WoniuCopyUtil;
import com.woniuxy.util.exception.AccountExistError;
import com.woniuxy.util.exception.accountException.AccountException;
import com.woniuxy.util.exception.code.AccountExceptionCode;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-15
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {
    @Resource
    private AccountDao accountDao;
    @Override
    public Result getAccountPages(AccountPageForm accountPageForm) {
        QueryWrapper<Account> queryWrapper =null;
        if (accountPageForm.getKeyWord()!=null&&accountPageForm.getKeyWord().trim()!=null) {
            queryWrapper=new QueryWrapper<Account>();
            queryWrapper.like("account_name",accountPageForm.getKeyWord());
        }
        Page<Account> page = page(new Page<>(accountPageForm.getPageIndex(), accountPageForm.getPageSize()), queryWrapper);
        List<SimpleAccountnfo> simpleAccountnfos = WoniuCopyUtil.copyMany(page.getRecords(), SimpleAccountnfo.class);
        IPage<SimpleAccountnfo> simpleAccountnfoIPage = new Page<SimpleAccountnfo>().setSize(page.getSize())
                .setRecords(simpleAccountnfos)
                .setTotal(page.getTotal())
                .setSize(page.getSize())
                .setPages(page.getPages());
        return Result.getInstance(IPage.class).setData(simpleAccountnfoIPage);
    }

    @Override
    public Result changPassword(ChaneAccountPasswordForm chaneAccountPasswordForm) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",chaneAccountPasswordForm.getAccountId());
        Account account = accountDao.selectOne(queryWrapper);
        if(account==null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_NOTFIND_ERROR);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if(!bCryptPasswordEncoder.matches(chaneAccountPasswordForm.getOldpass().trim(),account.getAccountPass())) throw new  AccountException(AccountExceptionCode.ACCOUNT_PASSWORD_ERROR);
        else{
            account.setAccountPass(bCryptPasswordEncoder.encode(chaneAccountPasswordForm.getPass()));
            accountDao.updateById(account);
            return Result.getInstance().setMsg("修改密码成功");
        }
    }

    @Override
    public Result<?> addAccount(RegistAccountForm registAccountForm) {

        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name",registAccountForm.getAccountName());
        Account account = accountDao.selectOne(queryWrapper);
        if(account!=null){
            throw new AccountExistError(AccountExceptionCode.ACCOUNT_Exist_ERROR);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String aa = bCryptPasswordEncoder.encode("aa");
        Account entity = WoniuCopyUtil.copyOne(registAccountForm, Account.class);
        entity.setAccountPass(aa);
        accountDao.insert(entity);

        return Result.getInstance().setMsg("添加成功");
    }


}
